package com.youlu.campus.website.service.impl;

import com.youlu.campus.website.entity.CampanyTopic;
import com.youlu.campus.website.entity.CampanyTopicPageQuery;
import com.youlu.campus.website.repositories.CampanyTopicRepository;
import com.youlu.campus.website.service.CampanyTopicService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;


/**
 * 公司主题业务逻辑层实现类
 *
 * @author su
 * @date 2025/08/25
 */
@Slf4j
@Service
public class CampanyServiceImpl implements CampanyTopicService {

    @Autowired
    private CampanyTopicRepository campanyTopicRepository;

    @Resource(name = "websiteMongoTemplate")
    private MongoTemplate mongoTemplate;

    @Override
    public CampanyTopic save(CampanyTopic campanyTopic) {
        if (StringUtils.isBlank(campanyTopic.getId())) {
            campanyTopic.setCreateTime(LocalDateTime.now());
            campanyTopic.setId(null);
        } else {
            campanyTopic.setUpdateTime(LocalDateTime.now());
        }
        return campanyTopicRepository.save(campanyTopic);
    }

    @Override
    public Boolean delete(String id) {
        Assert.hasText(id, "合作伙伴 ID 不能为空");
        campanyTopicRepository.deleteById(id);
        return true;
    }

    @Override
    public CampanyTopic findById(String id) {
        return campanyTopicRepository.findById(id).orElse(null);
    }

    @Override
    public Page<CampanyTopic> findByPage(CampanyTopicPageQuery pageQuery) {
        Query query = new Query();
        if (StringUtils.isNotBlank(pageQuery.getTitle())) {
            query.addCriteria(Criteria.where("title").regex(pageQuery.getTitle(), "i"));
        }
        //统计总条数（分页需要先查总数）
        long total = mongoTemplate.count(query, CampanyTopic.class);

        Pageable pageRequest = PageRequest.of(pageQuery.getPage() - 1, pageQuery.getPageSize(), Sort.by("createTime").descending());
        List<CampanyTopic> list;
        if (total == 0) {
            list = Arrays.asList();
        } else {
            // 应用分页（设置跳过的条数和查询的条数）
            query.skip(pageRequest.getOffset()).limit(pageRequest.getPageSize());
            // 应用排序
            query.with(pageRequest.getSort());
            list = mongoTemplate.find(query, CampanyTopic.class);
        }
        return PageableExecutionUtils.getPage(list, pageRequest, () -> total);
    }

    @Override
    public List<CampanyTopic> findAll() {
        return campanyTopicRepository.findAll();
    }
}